﻿using H.Utils.DataStruct;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace H.Utils
{
    /// <summary>
    /// Ini读写工具
    /// </summary>
    public static class INIUtil
    {
        internal static class NativeMethods
        {
            [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
            internal static extern int GetPrivateProfileInt(
                string lpAppName,
                string lpKeyName,
                int nDefault,
                string lpFileName
                );

            [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
            internal static extern int GetPrivateProfileString(
                string lpAppName,
                string lpKeyName,
                string lpDefault,
                StringBuilder lpReturnedString,
                int nSize,
                string lpFileName
                );

            [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
            internal static extern int WritePrivateProfileString(
                string lpAppName,
                string lpKeyName,
                string lpString,
                string lpFileName
                );
        }

        /// <summary>
        /// 缓冲并重用StringBuilder对象
        /// </summary>
        private static readonly CommonPool<StringBuilder> _Pool = new CommonPool<StringBuilder>() { MaxCount = 10 };

        /// <summary>
        /// 从INI文件读取一个值整数值
        /// </summary>
        /// <param name="iniFile">被操作的INI文件的路径</param>
        /// <param name="sectionName">INI文件中被操作的节名</param>
        /// <param name="keyName">INI文件中被操作的键</param>
        /// <param name="defaultValue">找不到值时返回的默认值</param>
        /// <returns>返回读取到的值</returns>
        public static int ReadInt(string iniFile, string sectionName, string keyName, int defaultValue)
        {
            return NativeMethods.GetPrivateProfileInt(sectionName, keyName, defaultValue, iniFile);
        }

        /// <summary>
        /// 从配置文件读取一个字符串
        /// </summary>
        /// <param name="iniFile">被操作的INI文件的路径</param>
        /// <param name="sectionName">INI文件中被操作的节名</param>
        /// <param name="keyName">INI文件中被操作的键</param>
        /// <param name="defaultValue">找不到值时返回的默认值</param>
        /// <returns>返回读取到的值</returns>
        public static string ReadString(string iniFile, string sectionName, string keyName, string defaultValue)
        {
            StringBuilder vRetSb = _Pool.Malloc();
            NativeMethods.GetPrivateProfileString(sectionName, keyName, defaultValue, vRetSb, 2048, iniFile);
            string ret = vRetSb.ToString();
            return ret;
        }

        /// <summary>
        /// 以整数的方式写入一个整数
        /// </summary>
        /// <param name="iniFile">被操作的INI文件的路径</param>
        /// <param name="sectionName">INI文件中被操作的节名</param>
        /// <param name="keyName">INI文件中被操作的键</param>
        /// <param name="value">希望写入的值</param>
        public static void WriteInt(string iniFile, string sectionName, string keyName, int value)
        {
            WriteString(iniFile, sectionName, keyName, value.ToString());
        }

        /// <summary>
        /// 以字符串方式写入一个字符串
        /// </summary>
        /// <param name="iniFile">被操作的INI文件的路径</param>
        /// <param name="sectionName">INI文件中被操作的节名</param>
        /// <param name="keyName">INI文件中被操作的键</param>
        /// <param name="value">希望写入的值</param>
        public static void WriteString(string iniFile, string sectionName, string keyName, string value)
        {
            NativeMethods.WritePrivateProfileString(sectionName, keyName, value, iniFile);
        }
    }
}
